package main

import (
	"context"
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/order_srv/proto"
)

var orderClient proto.OrderClient
var Conn *grpc.ClientConn

/*
	func TestCreatCartItem(userId int32, goodsId int32, nums int32) {
		_, err := orderClient.CreateCartItem(context.Background(), &proto.CartItemRequest{
			UserId:  userId,
			Nums:    nums,
			GoodsId: goodsId,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("创建购物车成功")
	}

	func TestCartItemList() {
		rsp, err := orderClient.CartItemList(context.Background(), &proto.UserInfo{
			Id: 1,
		})
		if err != nil {
			panic(err)
		}
		for _, item := range rsp.Data {
			fmt.Println(item.Id, item.GoodsId, item.Nums)
		}
		fmt.Println()
	}

	func TestUpdateCartItem(id int32) {
		_, err := orderClient.UpdateCartItem(context.Background(), &proto.CartItemRequest{
			Id:      id,
			Checked: true,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("更新购物车成功")
	}

	func TestCreateOrder() {
		_, err := orderClient.CreateOrder(context.Background(), &proto.OrderRequest{
			UserId:  1,
			Address: "北京市海淀区西二旗",
			Name:    "张三",
			Mobile:  "13800138000",
			Post:    "请尽快发货",
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("创建订单成功")
	}

	func TestGetOrderDetail(orderId int32) {
		rsp, err := orderClient.OrderDetail(context.Background(), &proto.OrderRequest{
			Id: orderId,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(rsp.OrderInfo.OrderSn)
		for _, item := range rsp.Goods {
			fmt.Println(item.GoodsName)
		}
	}
*/
func TestOrderList() {
	rsp, err := orderClient.OrderList(context.Background(), &proto.OrderFilterRequest{UserId: 2})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)
	for _, item := range rsp.Data {
		fmt.Println(item.OrderSn)
	}
}
func Init() {
	var err error
	Conn, err = grpc.NewClient("192.168.175.1:52639", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	orderClient = proto.NewOrderClient(Conn)
}

func main() {
	Init()

	//TestCreatCartItem(1, 2, 1)
	//TestCartItemList()
	//TestGetOrderDetail(1)
	TestOrderList()
	defer func() {
		err := Conn.Close()
		if err != nil {
			zap.S().Errorf("conn.close错误")
		}
	}()

}
